package com.app.web.front.controller;

import java.util.List;

import com.app.util.AjaxResult;
import com.app.util.DateUtils;
import com.app.web.BaseController;
import com.app.web.front.model.BlogCategory;
import com.app.web.front.model.Category;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

public class BlogCategoryController extends BaseController{
	
	public void findListByUid(){
		AjaxResult result = new AjaxResult(1,"");
		try {
			int userId = this.getCurrUser().getInt("id");
			List<Record> blogCategorys = BlogCategory.dao.findListByUid(userId);
			
			result.setData("datas", blogCategorys);
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg(0, "类别加载失败："+e.getMessage());
		}finally{
			this.renderJson(result.toJson());
		}
	}
	
	public void findCatesByUid(){
		AjaxResult result = new AjaxResult(1);
		int userId = this.getCurrUser().getInt("id");
		result.setData("datas", BlogCategory.dao.getCateTree(userId));
		this.renderJson(result.toJson());
	}
	public void addOrUpdate() throws Exception{
		AjaxResult result = new AjaxResult(1,"操作成功");
		try {
			BlogCategory blogCategory = this.getModel(BlogCategory.class).setAttrs(this.getParamMap());
			if(StrKit.notNull(blogCategory.get("id"))){
				blogCategory.update();
			}else{
				blogCategory.set("user_id", this.getCurrUser().get("id"));
				blogCategory.set("create_time", DateUtils.getCurrDateTime());
				blogCategory.save();
			}
			result.setData("blog_category", BlogCategory.dao.getBlogCategory(blogCategory.get("id")));
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg(0, "提交失败："+e.getMessage());
		}finally{
			this.renderJson(result.toJson());
		}
	}
	
	public void deleteById(){
		AjaxResult result = new AjaxResult(1,"删除成功");
		try {
			BlogCategory.dao.deleteById(this.getPara("id"));
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg(0, "删除失败");
		}
		
		this.renderJson(result.toJson());
	}
}
